using System.Collections.Generic;

namespace Game.Plugin.Parse
{
    public class PlusOperator : IExpression 
    {
        readonly ICodeObject leftOperand;
        readonly ICodeObject rightOperand;

        public PlusOperator(
            ICodeObject left,
            ICodeObject right) 
        { 
            leftOperand = left; 
            rightOperand = right;
        }

        public object Interpret(Dictionary<string, ICodeObject> variables)
        {
            object x = leftOperand.Interpret(variables);
            object y = rightOperand.Interpret(variables);
            if (x is int && y is int)
            {
                return (int)x + (int)y;
            }

            return new ErrorObject();
        }

    }

    public class PlusParser : IParser
    {
        public bool CanParse(string token)
        {
            return (token == "+");
        }

        public ICodeObject Parse(
            string token,
            ParserContext context)
        {
            IExpression e = new PlusOperator(
                context.Stack.Pop(),
                context.Stack.Pop());
            context.Stack.Push(e);
            return e;
        }
    }

}